<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $settings, $user_info;
global $dbtables, $allow_fullscan, $fullscan_cost;
	
// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

bigtitle($txt['bnt_lrs_title']);

if (isset($_GET['sector']))
	$sector = $_GET['sector'];
else
{
	echo '
		<p>', $txt['bnt_lrs_cantscan'], '</p>';
	return;
}

srand((double)microtime() * 1000000);

// Get user info.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Set number of links to 0 ahead of time to avoid undefined variable later on.
// This would only occur if the player was doing a full scan in sector without any links.
$num_links = 0;

if ($sector == "*")
{
	if( !$allow_fullscan)
	{
		echo '
			<p>', $txt['bnt_lrs_nofull'], '</p>';
		return;
	}
	
	if ($playerinfo['turns'] < $fullscan_cost)
	{
		$txt['bnt_lrs_noturns'] = str_replace("[fullscan_cost]", $fullscan_cost, $txt['bnt_lrs_noturns']);
		echo '
			<p>', $txt['bnt_lrs_noturns'], '</p>';
		return;
	}

	$txt['bnt_lrs_cost'] = str_replace("[fullscan_cost]", $fullscan_cost, $txt['bnt_lrs_cost']);	
	echo '
			<p>', $txt['bnt_lrs_cost'] ,' ', NUMBER($fullscan_cost), '</p>';

	// Deduct the appropriate number of turns.
	$smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET turns = turns - {int:fullscan_cost},
			turns_used = turns_used + {int:fullscan_cost}
		WHERE ship_id = {int:ship_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'ship_id' => $playerinfo['ship_id'],
			'fullscan_cost' => $fullscan_cost
		)
	);

	// Get sectors which can be reached from the player's current sector.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_links}
		WHERE link_start = {int:sector_id}
		ORDER BY link_dest',
		array(
			'db_links' => $dbtables['links'],
			'sector_id' => $playerinfo['sector']
		)
	);
	
	echo '
		<script type="text/javascript">
			$(document).ready(function(){
				// Table sorting function
				$("#sector_scan_table").tablesorter({ 
					widgets: ["zebra"]
				});
			});
		</script>
		<table id="sector_scan_table" class="bnt_table">
		<thead>
			<tr>
				<th>', $txt['bnt_sector'], '</th>
				<th>', $txt['bnt_lrs_links'], '</th>
				<th>', $txt['bnt_lrs_ships'], '</th>
				<th>', $txt['bnt_port'], '</th>
				<th>', $txt['bnt_planets'], '</th>
				<th>', $txt['bnt_mines'], '</th>
				<th>', $txt['bnt_fighters'], '</th>';
		
	if ($playerinfo['dev_lssd'] == 'Y')
		echo '
				<th>', $txt['bnt_lss'], '</th>';

	echo '
			</tr>
		</thead>
		<tbody>';
	
	while ($row = $smcFunc['db_fetch_assoc']($result))
	{
		// Get number of sectors which can be reached from scanned sector.
		$result2 = $smcFunc['db_query']('', '
			SELECT COUNT(*) AS count
			FROM {raw:db_links}
			WHERE link_start = {int:link_dest}',
			array(
				'db_links' => $dbtables['links'],
				'link_dest' => $row['link_dest']
			)
		);
		$row2 = $smcFunc['db_fetch_assoc']($result2);
		$num_links = $row2['count'];
		$smcFunc['db_free_result']($result2);

		// Get number of ships in scanned sector.
		$result2 = $smcFunc['db_query']('', '
			SELECT COUNT(*) AS count
			FROM {raw:db_ships}
			WHERE sector = {int:link_dest}
			AND on_planet = "N"
			AND ship_destroyed = "N"',
			array(
				'db_ships' => $dbtables['ships'],
				'link_dest' => $row['link_dest']
			)
		);
		$row2 = $smcFunc['db_fetch_assoc']($result2);
		$num_ships = $row2['count'];
		$smcFunc['db_free_result']($result2);

		// Get port type and discover the presence of a planet in scanned sector.
		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_universe}
			WHERE sector_id = {int:link_dest}',
			array(
				'db_universe' => $dbtables['universe'],
				'link_dest' => $row['link_dest']
			)
		);
		
		$result3 = $smcFunc['db_query']('', '
			SELECT planet_id
			FROM {raw:db_planets}
			WHERE sector_id = {int:link_dest}',
			array(
				'db_planets' => $dbtables['planets'],
				'link_dest' => $row['link_dest']
			)
		);
		
		$resultSDa = $smcFunc['db_query']('', '
			SELECT SUM(quantity) AS mines
			FROM {raw:db_sector_defence}
			WHERE sector_id = {int:link_dest}
			AND defence_type = "M"',
			array(
				'db_sector_defence' => $dbtables['sector_defence'],
				'link_dest' => $row['link_dest']
			)
		);
		
		$resultSDb = $smcFunc['db_query']('', '
			SELECT SUM(quantity) AS fighters
			FROM {raw:db_sector_defence}
			WHERE sector_id = {int:link_dest}
			AND defence_type = "F"',
			array(
				'db_sector_defence' => $dbtables['sector_defence'],
				'link_dest' => $row['link_dest']
			)
		);
		
		$sectorinfo = $smcFunc['db_fetch_assoc']($result2);
		$defM = $smcFunc['db_fetch_assoc']($resultSDa);
		$defF = $smcFunc['db_fetch_assoc']($resultSDb);
		$port_type = $sectorinfo['port_type'];
		$has_planet = $smcFunc['db_num_rows']($result3);
		$has_mines = NUMBER($defM['mines']);
		$has_fighters = NUMBER($defF['fighters']);
		
		$smcFunc['db_free_result']($result2);
		$smcFunc['db_free_result']($result3);
		$smcFunc['db_free_result']($resultSDa);
		$smcFunc['db_free_result']($resultSDb);

		echo '
				<tr>
					<td><a class="warp_new_button" href="#">', $row['link_dest'], '</a></td>
					<td>', $num_links, '</td>
					<td>', $num_ships, '</td>
					<td>',t_port_icon($port_type), '</td>
					<td>', $has_planet, '</td>
					<td>', $has_mines, '</td>
					<td>', $has_fighters, '</td>';
		
		if ($playerinfo['dev_lssd'] == 'Y')
		{
			$dev_lssd_result = $smcFunc['db_query']('', '
				SELECT *
				FROM {raw:db_movement_log}
				WHERE ship_id <> {int:ship_id}
				AND sector_id = {int:link_dest}
				ORDER BY time DESC
				LIMIT 1',
				array(
					'db_movement_log' => $dbtables['movement_log'],
					'ship_id' => $playerinfo['ship_id'],
					'link_dest' => $row['link_dest']
				)
			);
			if ($smcFunc['db_num_rows']($dev_lssd_result) == 0)
				echo '
					<td>', $txt['bnt_none'], '</td>';
			else
			{
				$row = $smcFunc['db_fetch_assoc']($dev_lssd_result);
				echo '
					<td>', get_player($row['ship_id']), '</td>';
			}
			$smcFunc['db_free_result']($dev_lssd_result);
		}
		
	    echo '
				</tr>';
	}
	
	$smcFunc['db_free_result']($result);
	
	echo '
				</tbody>
			</table>';

	if ($num_links == 0)
		echo '
			<p>', $txt['bnt_none'], '.</p>';
}
else
{
	// User requested a single sector (standard) long range scan.

	// Get scanned sector information
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_universe}
		WHERE sector_id = {int:sector_id}',
		array(
			'db_universe' => $dbtables['universe'],
			'sector_id' => $sector
		)
	);
	$sectorinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);

	// Get sectors which can be reached through scanned sector.
	$result = $smcFunc['db_query']('', '
		SELECT link_dest
		FROM {raw:db_links}
		WHERE link_start = {int:sector_id}
		ORDER BY link_dest ASC',
		array(
			'db_links' => $dbtables['links'],
			'sector_id' => $sector
		)
	);

	$i = 0;

	if ($smcFunc['db_num_rows']($result) > 0)
	{
		while ($row = $smcFunc['db_fetch_assoc']($result))
		{
			$links[$i] = $row['link_dest'];
			$i++;
		}
	}
	$num_links = $i;
	$smcFunc['db_free_result']($result);
	
	// get sectors which can be reached from the player's current sector
	$result = $smcFunc['db_query']('', '
		SELECT link_dest
		FROM {raw:db_links}
		WHERE link_start = {int:sector_id}
		ORDER BY link_dest ASC',
		array(
			'db_links' => $dbtables['links'],
			'sector_id' => $playerinfo['sector']
		)
	);

	$i = 0;
	$flag = 0;

	if ($smcFunc['db_num_rows']($result) > 0)
	{
		while ($row = $smcFunc['db_fetch_assoc']($result))
		{
			if ($row['link_dest'] == $sector)
				$flag = 1;
			$i++;
		}
	}
	$smcFunc['db_free_result']($result);
	
	if ($flag == 0)
	{
		echo '
			<p>', $txt['bnt_lrs_cantscan'], '</p>';
		return;
	}

	echo '
	`	<input id="scan_cost" type="hidden" value="0" />
		<p><strong<h3>', $txt['bnt_sector'], ' ', $sector;
		
	if ($sectorinfo['sector_name'] != "")
		echo ' (', $sectorinfo['sector_name'], ')';

	echo '</h3></strong</p>
	
		<p><h3>', $txt['bnt_links'], '</h3>';
				
	if($num_links == 0)
	{
		echo $txt['bnt_none'];
	}
	else
	{
		for ($i = 0; $i < $num_links; $i++)
		{
			echo $links[$i];
			if ($i + 1 != $num_links)
				echo ", ";
		}
	}
	
	echo '</p>
	
		<p><h3>', $txt['bnt_ships'], '</h3>
		<ul>';
	
	if ($sector != 0)
	{
		// Get ships located in the scanned sector.
		$result = $smcFunc['db_query']('', '
			SELECT ship_id, ship_name, character_name, cloak
			FROM {raw:db_ships}
			WHERE sector = {int:sector_id}
			AND on_planet="N"',
			array(
				'db_ships' => $dbtables['ships'],
				'sector_id' => $sector
			)
		);
		
		if ($smcFunc['db_num_rows']($result) > 0)
			echo '
			<li>', $txt['bnt_none'], '</li>';
		else
		{
			$num_detected = 0;
			while ($row = $smcFunc['db_fetch_assoc']($result))
			{
				// Display other ships in sector - unless they are successfully cloaked
				$success = SCAN_SUCCESS($playerinfo['sensors'], $row['cloak']);
				if ($success < 5)
					$success = 5;
				if ($success > 95)
					$success = 95;
				$roll = rand(1, 100);
				if ($roll < $success)
				{
					$num_detected++;
					
					echo '
			<li>', $row['ship_name'], '(', $row['character_name'], ')</li>';
			
				}
			}
			$smcFunc['db_free_result']($result);
			if (!$num_detected)
				echo '
			<li>', $txt['bnt_none'], '</li>';
		}
	}
	else
		echo '
			<li>', $txt['bnt_lrs_zero'], '</li>';

	echo '
		</ul>
		</p>
		<p><h3>', $txt['bnt_port'], '</h3>';
		
	if ($sectorinfo['port_type'] == "none")
	{
		echo $txt['bnt_none'];
	}
	else
	{
		if ($sectorinfo['port_type'] != "none")
		{
			$port_type = $sectorinfo['port_type'];
			$icon_alt_text = ucfirst(t_port($port_type));
			$icon_port_type_name = $port_type . ".gif";
			$image_string = "<img align=absmiddle height=12 width=12 alt=\"$icon_alt_text\" src=\"$settings[images_url]/bnt/$icon_port_type_name\">";
		}
		echo $image_string, ' ', t_port($sectorinfo['port_type']);
	}
	
	echo '</p>
		<p><h3>', $txt['bnt_planets'], '</h3>';
	
	$result = $smcFunc['db_query']('', '
		SELECT name, owner
		FROM {raw:db_planets}
		WHERE sector_id = {int:sector_id}',
		array(
			'db_planets' => $dbtables['planets'],
			'sector_id' => $sectorinfo['sector_id']
		)
	);

	if ($smcFunc['db_num_rows']($result) == 0)
		echo $txt['bnt_none'];
	else
	{
		while ($row = $smcFunc['db_fetch_assoc']($result))
		{
			$planet = $row;
			if (empty($planet['name']))
				echo $txt['bnt_unnamed'];
			else
				echo $planet['name'];

			if($planet['owner'] == 0)
				echo ' (', $txt['bnt_unowned'], ')';
			else
			{
				$result2 = $smcFunc['db_query']('', '
					SELECT character_name
					FROM {raw:db_ships}
					WHERE ship_id = {int:ship_id}
					AND on_planet="N"',
					array(
						'db_ships' => $dbtables['ships'],
						'ship_id' => $planet['owner']
					)
				);
				$planet_owner_name = $smcFunc['db_fetch_assoc']($result2);
				$smcFunc['db_free_result']($result2);
				
				echo ' (', $planet_owner_name['character_name'], ')';
			}
		}
		$smcFunc['db_free_result']($result);
	}
	
	$resultSDa = $smcFunc['db_query']('', '
		SELECT SUM(quantity) as mines
		FROM {raw:db_sector_defence}
		WHERE sector_id = {int:sector_id}
		AND defence_type = "M"',
		array(
			'db_sector_defence' => $dbtables['sector_defence'],
			'sector_id' => $sector
		)
	);
	
	$resultSDb = $smcFunc['db_query']('', '
		SELECT SUM(quantity) as fighters
		FROM {raw:db_sector_defence}
		WHERE sector_id = {int:sector_id}
		AND defence_type = "F"',
		array(
			'db_sector_defence' => $dbtables['sector_defence'],
			'sector_id' => $sector
		)
	);
	
	$defM = $smcFunc['db_fetch_assoc']($resultSDa);
	$defF = $smcFunc['db_fetch_assoc']($resultSDb);
	$smcFunc['db_free_result']($resultSDa);
	$smcFunc['db_free_result']($resultSDb);

	echo '</p>
		<p><h3>', $txt['bnt_mines'], '</h3>
			', NUMBER($defM['mines']), '
		</p>
		<p><h3>', $txt['bnt_fighters'], '</h3>
			', NUMBER($defF['fighters']), '
		</p>';
	
	if ($playerinfo['dev_lssd'] == 'Y')
	{
		echo '
		<p><h3>', $txt['bnt_lss'], '</h3>';
		
		$dev_lssd_result = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_movement_log}
			WHERE ship_id <> {int:ship_id}
			AND sector_id = {int:sector_id}
			ORDER BY time DESC
			LIMIT 1',
			array(
				'db_movement_log' => $dbtables['movement_log'],
				'ship_id' => $playerinfo['ship_id'],
				'sector_id' => $sector
			)
		);
		
		if ($smcFunc['db_num_rows']($dev_lssd_result) == 0)
			echo $txt['bnt_none'];
		else
		{
			$row = $smcFunc['db_fetch_assoc']($dev_lssd_result);
			$smcFunc['db_free_result']($dev_lssd_result);
			echo get_player($row['ship_id']);
		}
		echo '</p>';
	}
}
			
// Get updated playerinfo
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Echo out hidden fields containing turns information for display update.
echo '
	<input id="lrscan_turns_available" type="hidden" value="', NUMBER($playerinfo['turns']), '" />
	<input id="lrscan_turns_used" type="hidden" value="', NUMBER($playerinfo['turns_used']), '" />';

?>
